﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;

namespace NET.Library.Web.UI
{
    /// <summary>
    /// Page is a custom implementation of the System.Web.UI.Page class user to override certain methods and add new functionality.
    /// </summary>
    public class Page : System.Web.UI.Page
    {
        #region Override Methods
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Overrides Render method to move viewstate, eventtarget and eventargument hidden inputs
        /// to end of page just before the end form tag.
        /// </summary>
        /// <param name="writer"></param>
        ///----------------------------------------------------------------------------------
        protected override void Render(HtmlTextWriter writer)
        {
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
            base.Render(htmlWriter);
            string html = stringWriter.ToString();
            int endPoint = 0;
            int startPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");

            if (startPoint >= 0)
            {

                endPoint = html.IndexOf("/>", startPoint) + 2;

                string viewstateInput = html.Substring(startPoint, endPoint - startPoint);

                html = html.Remove(startPoint, endPoint - startPoint);

                int FormEndStart = html.IndexOf("</form>") - 1;

                if (FormEndStart >= 0)
                {

                    html = html.Insert(FormEndStart, viewstateInput);

                }

            }

            startPoint = html.IndexOf("<input type=\"hidden\" name=\"__EVENTTARGET\"");

            if (startPoint >= 0)
            {
                endPoint = html.IndexOf("/>", startPoint) + 2;
                string eventtargetInput = html.Substring(startPoint, endPoint - startPoint);

                html = html.Remove(startPoint, endPoint - startPoint);

                int FormEndStart = html.IndexOf("</form>") - 1;

                if (FormEndStart >= 0)
                {

                    html = html.Insert(FormEndStart, eventtargetInput);

                }
            }



            startPoint = html.IndexOf("<input type=\"hidden\" name=\"__EVENTARGUMENT\"");

            if (startPoint >= 0)
            {
                endPoint = html.IndexOf("/>", startPoint) + 2;
                string eventargumentInput = html.Substring(startPoint, endPoint - startPoint);

                html = html.Remove(startPoint, endPoint - startPoint);

                int FormEndStart = html.IndexOf("</form>") - 1;

                if (FormEndStart >= 0)
                {

                    html = html.Insert(FormEndStart, eventargumentInput);

                }
            }

            writer.Write(html);
        }
        #endregion
    }
}
